package com.whisperly.echotalk.starryb.utils;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.LocaleList;
import android.preference.PreferenceManager;
import android.util.Log;

import com.whisperly.echotalk.bllmodel.UserWithChat;

import java.util.Locale;

import cn.bmob.v3.Bmob;

public class Init extends Application {

    private static Context context;
    private static Init instance;
    private static UserWithChat user;
    private static String shareUrl = "";

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        instance = this;
        Bmob.resetDomain("https://blinkchat.chen88.top/8/");
        Bmob.initialize(this, "9eebd53ecad249b492b549f4efce46f0");
        Log.e("初始化","执行了");

        // 从持久化存储（如SharedPreferences）加载保存的语言设置
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String languageCode = prefs.getString("language", "en");
        // 设置语言
        updateResources(this, languageCode);
    }
    public void updateResources(Context context, String languageCode) {
        Locale locale = new Locale(languageCode);
        Locale.setDefault(locale);

        Resources res = context.getResources();
        Configuration config = new Configuration(res.getConfiguration());
        config.setLocale(locale);
        LocaleList localeList = new LocaleList(locale);
        LocaleList.setDefault(localeList);
        config.setLocales(localeList);
        context.createConfigurationContext(config);

    }

    public static Context getContext() {
        return context;
    }

    public static void setContext(Context context) {
        Init.context = context;
    }

    public static Init getInstance() {
        return instance;
    }

    public static void setInstance(Init instance) {
        Init.instance = instance;
    }

    public static UserWithChat getUser() {
        return user;
    }

    public static void setUser(UserWithChat user) {
        Init.user = user;
    }

    public static String getShareUrl() {
        return shareUrl;
    }

    public static void setShareUrl(String shareUrl) {
        Init.shareUrl = shareUrl;
    }
}
